package com.yilin.platform.controller;

import com.google.gson.reflect.TypeToken;
import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.commons.utils.MapUtil;
import com.yilin.platform.base_core.interceptor.SkipLoginVerify;
import com.yilin.platform.entity.user.deserialize.HatchPlatProduct;
import com.yilin.platform.service.IUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * IOS 访问  applinks 文件
 */

@RestController
@RequestMapping("/xbr/")
public class AppController extends BaseController {
    @Value("${plat-product-code}")
    private String platProductCode;//平台产品代码
    @Value("${ios-developer-group-id}")
    private String iosDeveloperGroupId;//平台产品代码

    @Resource
    IUserService userService;

    /**
     * 小镖人目前，其他孵化平台自定
     * 开发团队ID：8WW78TXGBA
     * 货主包名：com.guizhouqibogultd.xiaoBiaoRen.huoZhu
     * 司机包名：com.guizhouqibogultd.xiaoBiaoRen.cheDui
     */
    @RequestMapping(value = "apple-app-site-association")
    @SkipLoginVerify
    public void app(HttpServletResponse response) {
        try {
            Map<String, Object> param = MapUtil.generate(map -> map.put("productCode", platProductCode));
            HatchPlatProduct product = userService.request("/user/hatch/getPlatProductHatchByCode", null, param, new TypeToken<HatchPlatProduct>() {});
            response.setCharacterEncoding("UTF-8");//设置字符集为'UTF-8'
            response.setContentType("application/json");//设置格式为json
            PrintWriter write = response.getWriter();
            String json = "{\"applinks\": {\"apps\": [],\"details\": [{\"appID\": \"%s.%s\",\"paths\": [\"/xbr_app_shipper/\"]},{\"appID\": \"%s.%s\",\"paths\": [\"/xbr_app_trucker/\"]}]}}";
            write.write(String.format(json, iosDeveloperGroupId, product.getShipperIosApplicationId(), iosDeveloperGroupId, product.getTruckerIosApplicationId()));
            write.flush();
            write.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
